package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.DeleteProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.shop2cn.iapi.agentproducts.service.DeleteProductCall;
import com.shop2cn.iapi.agentproducts.service.DeleteProductCallV2;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.business.service.TradingForDGSQCallService;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;

/**
*删除商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DeleteProduct{
	private static DeleteProductBean deleteproductBean;
	private static DeleteProductCall deleteproductCall;
	//渠道商
	private static int sellerId = AgentUsers.rootUserId;
	private static String sellerName = "NickautoTest";
	//渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	//渠道商->代理商2
	private static int agentUserId2 = AgentUsers.agentUserId2;
	private static String agentUserName2 = "NickautoTest";

	// 渠道商
			private static int rootUserId = AgentUsers.rootUserId;
			private static String rootUserName = "NickautoTest";
			
	//vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	//vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	//vip渠道->代理->代理
	private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
	private static String vipAgentUserName2 = "NickautoTest";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除商品");
	}

	@Before
	public void caseUp() {
		deleteproductBean=new DeleteProductBean();
		deleteproductCall=new DeleteProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deleteproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	public void verifyDB(List<String> productIds) throws InterruptedException {//isRoot是否是渠道商
		Thread.sleep(3000);
		//同步服务会将action改成-2
		productIds.forEach(p->{
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(p);
			Logger.verifyEquals(-2,agentProductPo.getAction(),"验证action");
			try {
				YmtProductsI ymtProductsI = ymtProductsIWapper.selectProductByProductId(p).get(0);
				Logger.verifyEquals(-2,ymtProductsI.getiAction(),"验证action");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});

	}
	public void verifyMongoCurMq(String pid,int count) throws ClientProtocolException, IOException, InterruptedException {
		Thread.sleep(2000);
		MessageListBean MessageListBean2 = MqQueryCallService.initAgentMongoCrud(pid);
		List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
		int redo=0;
		while(joa2.size()<1&&redo<3) {
			Thread.sleep(5000);
			joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
			redo++;
		}
		Logger.debug(joa2);
		Logger.verifyEquals(count,joa2.size(),"验证发送两条消息到总线");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_01() {
		Logger.start(true,"渠道商删除商品,删除成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			deleteproductBean.setProductId(productId);
			deleteproductBean.setUserId(sellerId);
			deleteproductCall.setData(deleteproductBean);
			deleteproductCall.callService();
			Logger.verifyEquals("200",deleteproductCall.getString("code"),"验证code");
			Thread.sleep(3000);
			verifyDB(Arrays.asList(productId));
			verifyMongoCurMq(productId,2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_02() {
		Logger.start(true,"代理商删除商品,删除成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,sellerId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			deleteproductBean.setUserId(agentUserId1);
			deleteproductBean.setProductId(agentProductId);
			deleteproductCall.setData(deleteproductBean);
			deleteproductCall.callService();
			Logger.verifyEquals("200",deleteproductCall.getString("code"),"验证code");
			Thread.sleep(3000);
			verifyDB(Arrays.asList(agentProductId));
			verifyMongoCurMq(agentProductId,2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_07() {
		Logger.start(true,"普通渠道->普通代理，渠道商删除商品,删除成功，代理商对应商品删除");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,sellerId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			deleteproductBean.setUserId(sellerId);
			deleteproductBean.setProductId(productId);
			deleteproductCall.setData(deleteproductBean);
			deleteproductCall.callService();
			Logger.verifyEquals("200",deleteproductCall.getString("code"),"验证code");
			Thread.sleep(3000);
			verifyDB(Arrays.asList(productId,agentProductId));
			verifyMongoCurMq(agentProductId,1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_03() {
		Logger.start(true,"vip渠道->vip1->vip2，vip渠道商删除商品，删除成功,vip1和VIP2对应商品信息删除");
		try {
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,true);

			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId2,vipAgentUserName2,agentProductId,vipAgentUserId1,false);
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);
			deleteproductBean.setUserId(vipRootUserId);
			deleteproductBean.setProductId(productId);
			deleteproductCall.setData(deleteproductBean);
			deleteproductCall.callService();
			Thread.sleep(3000);
			Logger.verifyEquals("200",deleteproductCall.getString("code"),"验证code");
			verifyMongoCurMq(productId,2);
			verifyDB(Arrays.asList(productId,agentProductId,agentProductId1));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_04() {
		Logger.start(true,"vip渠道->vip1->vip2，vip1删除商品，删除成功，vip2商品删除");
		try {
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,true);

			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId2,vipAgentUserName2,agentProductId,vipAgentUserId1,false);
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);
			deleteproductBean.setUserId(vipAgentUserId1);
			deleteproductBean.setProductId(agentProductId);
			deleteproductCall.setData(deleteproductBean);
			deleteproductCall.callService();
			Thread.sleep(3000);
			Logger.verifyEquals("200",deleteproductCall.getString("code"),"验证code");
			verifyDB(Arrays.asList(agentProductId,agentProductId1));
			verifyMongoCurMq(agentProductId,2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_05() {
		Logger.start(true,"商品id不存在,删除失败");
		try {
			deleteproductBean.setUserId(vipAgentUserId1);
			deleteproductBean.setProductId("111");
			deleteproductCall.setData(deleteproductBean);
			deleteproductCall.callService();
			Logger.verifyEquals("201",deleteproductCall.getString("code"),"验证code");
			Logger.verifyEquals("商品不存在",deleteproductCall.getString("message"),"验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_06() {
		Logger.start(true,"用户id不存在，删除失败");
		try {
			deleteproductBean.setUserId(111);
			deleteproductBean.setProductId("111");
			deleteproductCall.setData(deleteproductBean);
			deleteproductCall.callService();
			Logger.verifyEquals("500",deleteproductCall.getString("code"),"验证code");
			Logger.verifyEquals("null",deleteproductCall.getString("message"),"验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_08() {
		Logger.start(true,"保存商品删除，删除成功");
		try {
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			agentProductBean.setSaleType(2);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			deleteproductBean.setUserId(vipRootUserId);
			deleteproductBean.setProductId(productId);
			deleteproductCall.setData(deleteproductBean);
			deleteproductCall.callService();
			Logger.verifyEquals("200",deleteproductCall.getString("code"),"验证code");
			verifyDB(Arrays.asList(productId));
			verifyMongoCurMq(productId,2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_09() {
		Logger.start(true,"渠道商客服删除商品，templateId4，删除成功");
		try {
			int csId = 500023286;
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			deleteproductBean.setUserId(csId);
			deleteproductBean.setProductId(productId);
			DeleteProductCallV2 deleteProductCallV2 = new DeleteProductCallV2();
			deleteProductCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D76799A6A0A7DFE5EABBBFADB332C40F560517F6D23115912D2A9D66752A159D46C5EF288EA2ACD7155EE0F7923987B3F24F5");

			deleteProductCallV2.setData(deleteproductBean);
			deleteProductCallV2.callService();
			verifyDB(Arrays.asList(productId));
			verifyMongoCurMq(productId,2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_10() {
		Logger.start(true,"渠道商客服删除商品，templateId3，删除成功");
		try {
			int csId = 500023287;
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			deleteproductBean.setUserId(csId);
			deleteproductBean.setProductId(productId);
			DeleteProductCallV2 deleteProductCallV2 = new DeleteProductCallV2();
			deleteProductCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D7679B8B5287DA8F6C358B032C5D4BF69995D777F8750D5FABCF6A03EBC4B67F0B3347F243B92D24DC8EAB34A7E1D0816E1CA");

			deleteProductCallV2.setData(deleteproductBean);
			deleteProductCallV2.callService();
			verifyDB(Arrays.asList(productId));
			verifyMongoCurMq(productId,2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_11() {
		Logger.start(true,"渠道商客服删除商品，templateId2，删除失败");
		try {
			int csId = 500023288;
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			deleteproductBean.setUserId(csId);
			deleteproductBean.setProductId(productId);
			DeleteProductCallV2 deleteProductCallV2 = new DeleteProductCallV2();
			deleteProductCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D767954A3F01B09D21A20479176177E62025B342BD096EA5F576089601F8445F4D7D4D699DCA8A26A1614D518EDA2019F0532");

			deleteProductCallV2.setData(deleteproductBean);
			deleteProductCallV2.callService();
			Logger.verifyEquals("201",deleteProductCallV2.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_12() {
		Logger.start(true,"渠道商客服删除商品，templateId1，删除失败");
		try {
			int csId = 500023289;
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			deleteproductBean.setUserId(csId);
			deleteproductBean.setProductId(productId);
			DeleteProductCallV2 deleteProductCallV2 = new DeleteProductCallV2();
			deleteProductCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D767915F5F242E8D33853E112C14B8904A6AE559730333EF9D3573D244B59588F70E2BE7FDEE32326B219044BC1E17CF19372");

			deleteProductCallV2.setData(deleteproductBean);
			deleteProductCallV2.callService();
			Logger.verifyEquals("201",deleteProductCallV2.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}


	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_13() {
		Logger.start(true,"代理客服，templateId5，删除成功");
		try {
			int csId = 500028917;
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);

			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,true);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			deleteproductBean.setUserId(csId);
			deleteproductBean.setProductId(agentProductId);
			DeleteProductCallV2 deleteProductCallV2 = new DeleteProductCallV2();
			deleteProductCallV2.addHeader("ymt-pars","accesstoken=FCD279E177169DBCB679B87B96425C7803B41D0A25FD8B897EC8BB361D5EB39B03B373ACE53EF9452E06A8E813D5317F9BA3A808D819FA27");

			deleteProductCallV2.setData(deleteproductBean);
			deleteProductCallV2.callService();
			verifyDB(Arrays.asList(agentProductId));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_14() {
		Logger.start(true,"代理客服，templateId6，无权限，发布失败");
		try {
			int csId = 500028918;
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);

			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,true);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			deleteproductBean.setUserId(csId);
			deleteproductBean.setProductId(agentProductId);
			DeleteProductCallV2 deleteProductCallV2 = new DeleteProductCallV2();
			deleteProductCallV2.addHeader("ymt-pars","accesstoken=FCD279E177169DBC07B81F1324B12582E11D0A2C48899B1B96AD63B1159A921BA1FF397A27188E27ECBD6B278EAB12F437EAD6743A323249");

			deleteProductCallV2.setData(deleteproductBean);
			deleteProductCallV2.callService();
			Logger.verifyEquals("201",deleteProductCallV2.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProduct_15() {
		Logger.start(true,"渠道商编辑商品，虚拟库存核销成功，删除规格成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					"", true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(0);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(2000);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
//			long orderId = Shop2cnStockCallService.InitReduceStockForOrder(sellerId,productDetailsDto.getCatalogList().get(0).getCatalogId(), productId, 50);
			
			//下单虚拟库存
			AgentProductBean productBean = new AgentProductBean();
			productBean.setProductId(productId);
			productBean.setRootProductId(productId);
			productBean.setCatalogList(Arrays.asList(productDetailsDto.getCatalogList().get(0).getCatalogId()));
			productBean.setGoodsOwnerId(Long.parseLong(rootUserId+""));
			productBean.setSellerId(rootUserId);
			productBean.setRootCatalogList(Arrays.asList(productDetailsDto.getCatalogList().get(0).getCatalogId()));
			productBean.setSellerLoginId(rootUserId+"");
			
			TradingServiceResp placeOrderResp = TradingForDGSQCallService
                    .placeOrder1(productBean, rootUserId, 2);
			
			
			Thread.sleep(2000);
			Shop2cnStockCallService.stockIn(rootUserId, productId, 100,1);
			
			Thread.sleep(2000);
			deleteproductBean.setUserId(rootUserId);
			deleteproductBean.setProductId(productId);
			deleteproductCall.setData(deleteproductBean);
			deleteproductCall.callService();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

}